/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "prs_FrmScrollBarStyle.h"

#include "prs_FrmSplitterStyle.h"
#include "prs_FrmNodes.h"
#include "prs_Unit.h"

using namespace prs;

// TRuleFrmHScrollBarStyle /////////////////////////////////////////////////////
//
TRuleFrmHScrollBarStyle::TRuleFrmHScrollBarStyle(
  snt::text::TContext &Context,
  svl::TVariant &VParentNode,
  TRuleFrmStyleState &RStyleState
) 
:vParentNode(VParentNode)
,rStyleState(RStyleState)
{
  snt::TReturn Return;
  snt::TFVoid F;
  snt::text::TNumber   N(Context);
  snt::text::TSequence T(Context);
  snt::text::TIdent	   I(Context);
  snt::text::TKeyword  K(Context);
  prs::TFuncNewNodeIntoAttrOldChildren FNewNode;

  this->Name(GetClassName()).Local(vParentNode)
  | K.New("hScrollBar") & FNewNode.New(VParentNode,TNodeFrmHScrollBarStyle()) & T.New("(") & A.New()
  ;
  
  A.Name(GetClassName() + "[A]")
  | T.New("scrollBar") & F.New(this,&TRuleFrmHScrollBarStyle::FScrollBar) & rStyleState.New() & A.New()
  | T.New("begButton") & T.New("=") & I.NewClosure(this,&TRuleFrmHScrollBarStyle::FBegButton) & A.New()
  | T.New("endButton") & T.New("=") & I.NewClosure(this,&TRuleFrmHScrollBarStyle::FEndButton) & A.New()
  | T.New("begPage")   & T.New("=") & I.NewClosure(this,&TRuleFrmHScrollBarStyle::FBegPage)   & A.New()
  | T.New("endPage")   & T.New("=") & I.NewClosure(this,&TRuleFrmHScrollBarStyle::FEndPage)   & A.New()
  | T.New("track")     & T.New("=") & I.NewClosure(this,&TRuleFrmHScrollBarStyle::FSplitter)  & A.New()
  | T.New("weight")    & T.New("=") & N.NewClosure(this,&TRuleFrmHScrollBarStyle::FWeight)    & A.New()
  | T.New(")") & Return.New()
  ;

}

//
frm::control::TButton::TStyle* TRuleFrmHScrollBarStyle::GetButtonStyle( const svl::TString &Style
													,       svl::TString &Errors ){
	TNodeFrmHScrollBarStyle* SBStyle = dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(SBStyle)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	TNode* style = Unit->SearchStyleById( Style );
	
	if( !style ){
		Errors << "Style " << Style << " not found.";
		return NULL;
	}

	TNodeFrmButtonStyle *button = dynamic_cast<TNodeFrmButtonStyle*>(
				TNodeSearch(style)
				.SearchChildren()
				.SearchByType(TNodeFrm::nodeType_Button)
				.GetFirst() );
	
	if( !button ){
		Errors << "Button style in " << Style << " not found.";
		return NULL;
	}

	return &button->Style.Value;
}

//
bool TRuleFrmHScrollBarStyle::FBegButton(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmHScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.BegButton = *style;
	
	return true;
}

//
bool TRuleFrmHScrollBarStyle::FEndButton(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmHScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.EndButton = *style;
	
	return true;
}

//
bool TRuleFrmHScrollBarStyle::FBegPage(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmHScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.BegPage = *style;
	
	return true;
}

//
bool TRuleFrmHScrollBarStyle::FEndPage(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmHScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.EndPage = *style;
	
	return true;
}

//
bool TRuleFrmHScrollBarStyle::FSplitter(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmHScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(SBStyle)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	TNode* style = Unit->SearchStyleById( Params.Ident.GetValue().String() );
	
	if( !style ){
		Params.OutError << "Style " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}

	TNodeFrmVSplitterStyle* vSplitter = dynamic_cast<TNodeFrmVSplitterStyle*>(
				TNodeSearch(style)
				.SearchChildren()
				.SearchByType(TNodeFrm::nodeType_VSplitter)
				.GetFirst() );
	
	if( !vSplitter ){
		Params.OutError << "hSplitter style in " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}
	
	SBStyle->Style.Value.Track = vSplitter->Style.Value;
	
	return true;
}

//
bool TRuleFrmHScrollBarStyle::FWeight(snt::text::TNumber::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmHScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmHScrollBarStyle*>(vParentNode.Pointer());
	
	SBStyle->Style.Value.Weight = Params.Number.GetValue().Integer();

	return true;
}


// TRuleFrmVScrollBarStyle /////////////////////////////////////////////////////
//
TRuleFrmVScrollBarStyle::TRuleFrmVScrollBarStyle(
  snt::text::TContext &Context,
  svl::TVariant &VParentNode,
  TRuleFrmStyleState &RStyleState
) 
:vParentNode(VParentNode)
,rStyleState(RStyleState)
{
  snt::TReturn Return;
  snt::TFVoid F;
  snt::text::TNumber   N(Context);
  snt::text::TSequence T(Context);
  snt::text::TIdent	   I(Context);
  snt::text::TKeyword  K(Context);
  prs::TFuncNewNodeIntoAttrOldChildren FNewNode;

  this->Name(GetClassName()).Local(vParentNode)
  | K.New("vScrollBar") & FNewNode.New(VParentNode,TNodeFrmVScrollBarStyle()) & T.New("(") & A.New()
  ;
  
  A.Name(GetClassName() + "[A]")
  | T.New("scrollBar") & F.New(this,&TRuleFrmVScrollBarStyle::FScrollBar) & rStyleState.New() & A.New()
  | T.New("begButton") & T.New("=") & I.NewClosure(this,&TRuleFrmVScrollBarStyle::FBegButton) & A.New()
  | T.New("endButton") & T.New("=") & I.NewClosure(this,&TRuleFrmVScrollBarStyle::FEndButton) & A.New()
  | T.New("begPage")   & T.New("=") & I.NewClosure(this,&TRuleFrmVScrollBarStyle::FBegPage)   & A.New()
  | T.New("endPage")   & T.New("=") & I.NewClosure(this,&TRuleFrmVScrollBarStyle::FEndPage)   & A.New()
  | T.New("track")     & T.New("=") & I.NewClosure(this,&TRuleFrmVScrollBarStyle::FSplitter)  & A.New()
  | T.New("weight")    & T.New("=") & N.NewClosure(this,&TRuleFrmVScrollBarStyle::FWeight)    & A.New()
  | T.New(")") & Return.New()
  ;

}

//
frm::control::TButton::TStyle* TRuleFrmVScrollBarStyle::GetButtonStyle( const svl::TString &Style
													,       svl::TString &Errors ){
	TNodeFrmVScrollBarStyle* SBStyle = dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(SBStyle)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	TNode* style = Unit->SearchStyleById( Style );
	
	if( !style ){
		Errors << "Style " << Style << " not found.";
		return NULL;
	}

	TNodeFrmButtonStyle *button = dynamic_cast<TNodeFrmButtonStyle*>(
				TNodeSearch(style)
				.SearchChildren()
				.SearchByType(TNodeFrm::nodeType_Button)
				.GetFirst() );
	
	if( !button ){
		Errors << "Button style in " << Style << " not found.";
		return NULL;
	}

	return &button->Style.Value;
}

//
bool TRuleFrmVScrollBarStyle::FBegButton(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmVScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.BegButton = *style;
	return true;
}

//
bool TRuleFrmVScrollBarStyle::FEndButton(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmVScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.EndButton = *style;
	
	return true;
}

//
bool TRuleFrmVScrollBarStyle::FBegPage(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmVScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.BegPage = *style;
	
	return true;
}

//
bool TRuleFrmVScrollBarStyle::FEndPage(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmVScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	frm::control::TButton::TStyle* style = GetButtonStyle( Params.Ident.GetValue().String(), Params.OutError );
	if ( !style ) return false;
	
	SBStyle->Style.Value.EndPage = *style;
	
	return true;
}

//
bool TRuleFrmVScrollBarStyle::FSplitter(snt::text::TIdent::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmVScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	TNodeUnit *Unit = dynamic_cast<TNodeUnit*>(
				TNodeSearch(SBStyle)
				.SearchNearParentByType(TNodeFrm::nodeType_Unit)
				.GetFirst() );
	
	TNode* style = Unit->SearchStyleById( Params.Ident.GetValue().String() );
	
	if( !style ){
		Params.OutError << "Style " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}

	TNodeFrmHSplitterStyle* hSplitter = dynamic_cast<TNodeFrmHSplitterStyle*>(
				TNodeSearch(style)
				.SearchChildren()
				.SearchByType(TNodeFrm::nodeType_HSplitter)
				.GetFirst() );
	
	if( !hSplitter ){
		Params.OutError << "hSplitter style in " << Params.Ident.GetValue().String() << " not found.";
		return false;
	}

	SBStyle->Style.Value.Track = hSplitter->Style.Value;

	return true;
}

//
bool TRuleFrmVScrollBarStyle::FWeight(snt::text::TNumber::TFuncClosureDoEntryParams &Params)
{
	TNodeFrmVScrollBarStyle* SBStyle = 
		dynamic_cast<TNodeFrmVScrollBarStyle*>(vParentNode.Pointer());
	
	SBStyle->Style.Value.Weight = Params.Number.GetValue().Integer();

	return true;
}